package com.ruoyi.common.core.condition;

import com.ruoyi.common.utils.ServletUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.*;

public class BaseCondition {

    private Long id;

    private Set<Long> ids;

    private Long idNot;

    private Ors ors;

    private Set<String> fields = new LinkedHashSet<>();

    private Map<String, String> more = new HashMap<>();

    private Set<String> groupBys = new LinkedHashSet<>();

    private Pageable pageable;

    public void limit(int limit) {

        if (this.pageable == null) {
            pageable = PageRequest.ofSize(limit);
        } else {
            pageable = PageRequest.of(0, 1, pageable.getSort());
        }
    }

    public Pageable getPageable() {
        return pageable;
    }

    public void setPageable(Pageable pageable) {
        this.pageable = pageable;
    }


    public Map<String, String> getMore() {
        return more;
    }

    public void setMore(Map<String, String> more) {
        this.more = more;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Set<Long> getIds() {
        return ids;
    }

    public void setIds(Set<Long> ids) {
        this.ids = ids;
    }

    public Long getIdNot() {
        return idNot;
    }

    public void setIdNot(Long idNot) {
        this.idNot = idNot;
    }

    public Set<String> getFields() {
        return fields;
    }

    public void setFields(Set<String> fields) {
        this.fields = fields;
    }

    public Set<String> getGroupBys() {
        return groupBys;
    }

    public void setGroupBys(Set<String> groupBys) {
        this.groupBys = groupBys;
    }

    public String getOrs() {
        if (ors == null) return null;
        String orsStr = ors.build();
        if (orsStr.isEmpty()) return null;
        return orsStr;
    }

    public void setOrs(Ors ors) {
        this.ors = ors;
    }

    /**
     * 工具方法
     * 将前端的search字符串转化为set ids
     *
     * @param search
     * @return
     */
    public Set<String> getSearchIds(String search) {
        if (search == null || "".equals(search)) {
            return null;
        }

        String v = search.replaceAll("[\\n\\s,，`;]+", ",");
        String[] vv = v.split(",");
        Set<String> set = new HashSet<>();
        for (String s : vv) {
            if (s == null || "".equals(s)) {
                continue;
            }
            set.add(s);
        }
        return set;
    }

    // 时间区间从全闭改为左闭右开，方便sql查询
    public List<Date> formatDateRangeWithZone(List<Date> daterange, ZoneId zone) {
        if (daterange.size() != 2)
            throw new RuntimeException("date range format error");

        Date end = daterange.get(1);

        // 如果时间有时分秒，则认为是按秒来查询，加1秒，如果没有，则认为是按天来查询，加1天
        ZonedDateTime openEnd = ZonedDateTime.ofInstant(end.toInstant(), zone);
        if (openEnd.getHour() == 0 && openEnd.getMinute() == 0 && openEnd.getSecond() == 0) {
            openEnd = openEnd.plusDays(1);
        } else {
            openEnd = openEnd.plusSeconds(1);
        }
        daterange.set(1, Date.from(openEnd.toInstant()));
        return daterange;
    }

    // 如果有时区公参，则使用时区公参
    public List<Date> formatDateRange(List<Date> daterange) {
        String tz = (String) ServletUtils.getRequestAttributes().getRequest().getAttribute("tz");
        if (tz == null || tz.isEmpty()) tz = ZoneId.systemDefault().getId();

        return formatDateRangeWithZone(daterange, ZoneId.of(tz));
    }
}
